#include <16F877A.H>
#device ADC=10
#include<stdio.h>
#include<stdlib.h>
//#define RTC PIN_B7
//#define MENU PIN_B6
//#define MENU PIN_B6

/*========================================== Controller Configuration Settings========================================*/
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
byte const menu[]={"\n\rEnter 1.Temperature 2.Turbitidy 3.pH 4.pH Calibration"};
byte const msg1[]={"\n\rTemperature reading is "};
byte const msg2[]={"\n\rTurbidity reading is "};
byte const msg3[]={"\n\rTurbidity exceeds the limit "};
byte const msg4[]={"\n\rTurbidity is in limit "};
byte const msg5[]={"\n\r pH Value is"};
byte const msg6[]={"\n\rPH Calibration Buffer Value = "};
//byte const msg7[]={"\n\r pH Reference Value is"};
byte const msg8[]={"\n\rPH Analog Voltage "};
int temperature=0,temp=0;
int PH_Ref_Analog;
int PH_Ref_Value;
int turbidity=0;
unsigned char Buffer[50]= {  0,};
int ph = 0;
char value;
int rem=0,quo=1,dummy=0,dum,ar[10],i,sr=0;
//#BYTE RCREG = 0x1A           //Receive char


//unsigned char Byte =0x00;
char *ptr;

void transmit(char a)
{

      putchar(a);
   delay_ms(100);

}
/*void SerialDataReceive()
{
	if(RCREG=='1' || RCREG=='2' ||RCREG=='3'||RCREG=='4')
   {// Store the received data byte by byte

	c++;
   }

}*/
void main()
{
   //enable_interrupts(INT_RDA);
   //enable_interrupts(GLOBAL);
  setup_port_A(ALL_ANALOG);
  setup_adc(ADC_CLOCK_INTERNAL);
   value=0;
   //c=0;

while(1)
{
  do
  {   //printf("\n\rEnter 1.Temperature 2.Turbitidy 3.pH 4.pH Calibration");

   for(i=0;menu[i]!='\0';i++)
   {
      transmit(menu[i]);
   }
   delay_ms(1000);
   value=getchar();
   delay_ms(100);
    //while(c==0);


}while(value!='1'&& value!='2'&& value!='3'&& value!='4');

    if(value=='1')
    {
      //printf("Temperature reading is ");
      for(i=0;msg1[i]!='\0';i++)
      {
      transmit(msg1[i]);
      }
      set_adc_channel(0);
      delay_us(10);
      temp=Read_ADC();
      temperature =  (5.0 * temp * 10.0) / 126 ;
      //printf("%d\n\r",temperature);
      i=0;
         dum=temperature;
         temperature=dum%100;
         temperature=temperature/10;
         dummy=temperature+48;
         //display(dummy);
         transmit(dummy);
         delay_us(10);
         temperature=dum%10;
         dummy=temperature+48;
         transmit(dummy);
         delay_us(10);
    }
   if(value=='2')
   {
//      printf("Turbidity reading is ");
      for(i=0;msg2[i]!='\0';i++)
      {
      transmit(msg2[i]);
      }
      set_adc_channel(1);
      delay_us(10);
      turbidity=Read_ADC();
//      printf("%d\n\r",turbidity);
         dum=turbidity;
         turbidity=dum%100;
         turbidity=turbidity/10;
         dummy=turbidity+48;
         //display(dummy);
         transmit(dummy);
         delay_us(10);
         turbidity=dum%10;
         dummy=turbidity+48;
         transmit(dummy);
         delay_us(10);

      if(dum<20)
      {
//          printf("Turbidity exceeds the limit");
         for(i=0;msg3[i]!='\0';i++)
         {
            transmit(msg3[i]);
         }
      }
      else
      {
//         printf("Turbidity is in limit");
           for(i=0;msg4[i]!='\0';i++)
            {
               transmit(msg4[i]);
            }
      }

   }
      if(value=='3')
      {
         set_adc_channel(2);
         delay_us(10);
         ph=Read_ADC();
//      printf("PH Value is ");
         for(i=0;msg5[i]!='\0';i++)
         {
            transmit(msg5[i]);
         }
        if(ph >= PH_Ref_Analog)
        {
            ph = ph -  PH_Ref_Analog;
            i = ph / 10;
            i = i + PH_Ref_Value;
//         printf("%d\n\r",i);
            dum=i;
            i=dum%100;
            i=i/10;
            dummy=i+48;
         //display(dummy);
            transmit(dummy);
            delay_us(10);
            i=dum%10;
            dummy=i+48;
            transmit(dummy);
            delay_us(10);
        }
        else
        {
          ph = PH_Ref_Analog - ph;
          i =  ph / 10;
          i =  PH_Ref_Value - i;
//          printf("%d",i);
         dum=i;
         i=dum%100;
         i=i/10;
         dummy=i+48;
         //display(dummy);
         transmit(dummy);
         delay_us(10);
         i=dum%10;
         dummy=i+48;
         transmit(dummy);
         delay_us(10);
        }
      }
      if(value=='4')
      {
         if(ptr != 0)
         {
            PH_Ref_Value = *(ptr + 6);
//        printf("PH Calibration Buffer Value = ");
         for(i=0;msg6[i]!='\0';i++)
         {
            transmit(msg6[i]);
         }
         i = PH_Ref_Value - 48;
         PH_Ref_Value = 7;
//        printf("%d",PH_Ref_Value);
         dum=i;
         
         i=dum%100;
         i=i/10;
         dummy=i+48;
         //display(dummy);
         transmit(dummy);
         delay_us(10);
         i=dum%10;
         dummy=i+48;
         transmit(dummy);
         delay_us(10);

        set_adc_channel(2);
        delay_us(10);
        temperature=Read_ADC();

        PH_Ref_Analog = ph;
        temperature = (5/1024)*temperature;
//        printf("PH Analog Voltage ");
       for(i=0;msg8[i]!='\0';i++)
         {
            transmit(msg8[i]);
         }
//       printf("%d",ph);
         dum=ph;
         ph=dum%100;
         ph=ph/10;
         dummy=ph+48;
         //display(dummy);
         transmit(dummy);
         delay_us(10);
         ph=dum%10;
         dummy=ph+48;
         transmit(dummy);
         delay_us(10);
      }
      }
    value=0;
}

}










